package com.ltz.service.well;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ltz.dao.well.WellGroupDao;
import com.ltz.model.Page;
import com.ltz.model.Pager;
import com.ltz.model.User;
import com.ltz.model.WellBasicData;
import com.ltz.model.WellGroup;

@Service("wellGroupService")
public class WellGroupService {
	@Resource
	private WellGroupDao wellGroupDao;
	//web
	public Page loadList(Page page,String queryStr){
		String sql="select wg.*,u.USERNAME as createName from mn_well_group wg left join mn_user u on wg.create_by=u.USERID where wg.well_group_name like ? order by create_date desc";
		return wellGroupDao.loadList(page,sql,new Object[]{"%"+queryStr+"%"});
	}
	
	public boolean doAddWellGroup(User user,String name){
		WellGroup wg=new WellGroup();
		wg.setWellGroupName(name);
		wg.setDateInput(new Date());
		wg.setOwner(user.getRealname());
		wellGroupDao.add(wg);
		return true;
	}
	public boolean doUpdateWellGroup(User user,int wellGroupId,String name){
		WellGroup wg=(WellGroup) wellGroupDao.get(WellGroup.class, wellGroupId);
		wg.setWellGroupName(name);
		wellGroupDao.update(wg);
		return true;
	}
	public boolean doDeleteWellGroup(int wellGroupId){
		String sql="update well_basic_data set group_id=null where group_id=?";
		wellGroupDao.excute(sql, wellGroupId);
		sql="delete from monthly_product_data where well_team_id=?";
		wellGroupDao.excute(sql, wellGroupId);
		wellGroupDao.delete(WellGroup.class, wellGroupId);
		return true;
	}
	//flex
	public Map<String, Object> findAllByPage(Pager pager) {
		Map<String, Object> map=new HashMap<String,Object>();
		Page page=new Page();
		page.setPageSize(pager.getPageSize());
		page.setPageNo(pager.getCurrPN());
		try{
			String sql="select wg.*,GROUP_CONCAT(w.name) wellNames from mn_well_group wg left join well_basic_data w on wg.well_group_id=w.group_id group by wg.well_group_id order by wg.date_input desc";
			page=wellGroupDao.findByPage(page,sql,null,WellGroup.class);
			pager.setTotalNo(page.getCount());
			map.put("data", page.getList());
			map.put("pager", pager);
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	public WellGroup save(WellGroup wg){
		wg.setWellGroupId(null);
		wg.setDateInput(new Date());
		wg=(WellGroup) wellGroupDao.add(wg);
		return wg;
	}
	public WellGroup update(WellGroup wg){
		wg=(WellGroup) wellGroupDao.update(wg);
		return wg;
	}
	public WellBasicData saveWell(WellBasicData w){
		w.setId(null);
		w.setDateInput(new Date());
		w=(WellBasicData) wellGroupDao.add(w);
		return w;
	}
	public Map<String,Object> findWellByGroup(int wellGroupId){
		Map<String,Object> map=new HashMap<String,Object>();
		//查出绑定的井
		String sql="select id,group_id,name,remark from well_basic_data where group_id=?";
		List list=wellGroupDao.nativeQueryForList(sql, new Object[]{wellGroupId}, WellBasicData.class);
		map.put("bindList", list);
		//闲置井
		sql="select id,group_id,name,remark from well_basic_data where group_id=0 or group_id is null";
		list=wellGroupDao.nativeQueryForList(sql, WellBasicData.class);
		map.put("unbindList", list);
		return map;
	}
	public Map<String,Object> bind(int wellId,int wellGroupId){
		String sql="update well_basic_data set group_id=? where id=?";
		wellGroupDao.excute(sql, wellGroupId,wellId);
		Map<String,Object> map=this.findWellByGroup(wellGroupId);
		return map;
	}
	public Map<String,Object> unbind(int wellId,int wellGroupId){
		String sql="update well_basic_data set group_id=0 where id=?";
		wellGroupDao.excute(sql, wellId);
		Map<String,Object> map=this.findWellByGroup(wellGroupId);
		return map;
	}
	public Map<String,Object> doDeleteWell(int wellId,int wellGroupId){
		String sql="delete from observation_record where well_id=?";
		wellGroupDao.excute(sql, wellId);
		wellGroupDao.delete(WellBasicData.class, wellId);
		Map<String,Object> map=this.findWellByGroup(wellGroupId);
		return map;
	}
}
